package cn.edu.baiyunu.myapplication.ui.home

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import cn.edu.baiyunu.myapplication.data.db.entity.PhotoRecord
import cn.edu.baiyunu.myapplication.data.model.PhotoSpec
import cn.edu.baiyunu.myapplication.data.model.PhotoSpecProvider
import cn.edu.baiyunu.myapplication.data.repository.PhotoRepository
import cn.edu.baiyunu.myapplication.data.repository.PreferenceRepository
import kotlinx.coroutines.flow.map

/**
 * 首页ViewModel，负责管理首页UI数据
 */
class HomeViewModel(
    private val preferenceRepository: PreferenceRepository,
    private val photoRepository: PhotoRepository
) : ViewModel() {
    
    // 热门规格列表
    val popularSpecs: List<PhotoSpec> = PhotoSpecProvider.getPopularSpecs()
    
    // 最近处理的照片
    val recentPhotos: LiveData<List<PhotoRecord>> = photoRepository
        .getRecentPhotoRecords(6)
        .asLiveData()
    
    // 默认规格类型
    val defaultSpecType: LiveData<String> = preferenceRepository
        .getDefaultSpecType()
        .asLiveData()
    
    // 获取特点列表
    fun getFeatures(): List<Feature> {
        return listOf(
            Feature(
                iconName = "magic",
                title = "智能修图",
                description = "AI自动优化照片，补光磨皮，提升画质"
            ),
            Feature(
                iconName = "crop-alt",
                title = "智能抠图",
                description = "精准识别人像，自动替换背景色"
            ),
            Feature(
                iconName = "download",
                title = "本地下载",
                description = "直接保存到您的设备"
            ),
            Feature(
                iconName = "shield-alt",
                title = "隐私保护",
                description = "照片本地处理，保护个人隐私"
            )
        )
    }
    
    // 获取使用流程
    fun getUsageFlows(): List<UsageFlow> {
        return listOf(
            UsageFlow(
                step = 1,
                iconName = "camera",
                title = "上传照片",
                description = "上传照片或使用相机拍摄"
            ),
            UsageFlow(
                step = 2,
                iconName = "ruler-combined",
                title = "选择规格",
                description = "选择所需证件照尺寸和底色"
            ),
            UsageFlow(
                step = 3,
                iconName = "sliders-h",
                title = "编辑照片",
                description = "智能修图或手动调整参数"
            ),
            UsageFlow(
                step = 4,
                iconName = "download",
                title = "下载保存",
                description = "生成高清照片并保存到设备"
            )
        )
    }
    
    // 用于表示首页特点卡片的数据类
    data class Feature(
        val iconName: String,
        val title: String,
        val description: String
    )
    
    // 用于表示使用流程的数据类
    data class UsageFlow(
        val step: Int,
        val iconName: String,
        val title: String,
        val description: String
    )
} 